Skip to content

UI: TextureRenderCore's renderUnitAnimationWithBlendingUnk at 100% - #480

Merged
gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-C-round17-100
Nov 24, 2026
Merged

gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-C-round17-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One function at 100%, from our draft in #363 (92.5% there), which started from gynt's asm draft. No header changes. It needs /GL with its one callee, so the PR adds a /GL line and a cmake/function-visibility.txt entry. One behaviour fix against his asm draft (below), already in #363.

Function Address Before reccmp raw Normalised
TextureRenderCore::renderUnitAnimationWithBlendingUnk 0x00451E00 92.5% (#363) 100.0% 100.0%

gynt's asm draft is 3c9bcdb (2026-10-04, "reimplement: SHC_3BB0A8C1_0x00451E00 91.5% Four asm loops recovered, stack slot allocation differs") on sourcehold's reimpl/TextureRenderCore. Before it he had the decompiler's C form at 6.0% (cb5b9c1 / f9dd597, 2026-10-02 and 2026-10-03), which is what #255 had when it closed. His newest version is 3d17527 (2026-11-02, "reimplement: swap if/else arms across UI/Rendering, BuildingMenus and Helpers", with d90dce4's style pass on top) on his fork's reimpl/UI-MenuItems-development. It is the decompiler's C form again (6.0%, no asm), so it isn't used here. 8ca7861 on his fork's reimpl/Wilps-2026-10-18 has the asm draft with the bug below.

Measured

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed).
  • Narrow build: this commit's file together with InitBlendFilterArraysUnk.cpp (its one callee, 100% on main), on main 0e4a83b: 100.0 raw and normalised, with no diff line. The whole listing is the same on both sides: the original's 864 instructions, with every operand the same, the stack slots included. In that build InitBlendFilterArraysUnk reads 97.9, as it does in a build of it alone without this file and in the full build below. That is reccmp's printing of its loop ends, addresses just past the table's end (below), not its code: the instructions are the same.
  • Full build: an all-on build of the previous cut of this branch on main 0e4a83b (the same code: this cut gives three parameters the header's names, tests isZoom2 by truthiness, changes three comments and puts the four asm blocks between // clang-format off and // clang-format on, and narrow builds of the two give byte-identical listings), with both resolver sets on and the branch's own /GL list (main's plus this file, next to InitBlendFilterArraysUnk.cpp, which main already lists), and with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out and its resolver off, as in main's baseline: 0 failed of 2,426 compile steps, 99.58 over 2,442 functions. renderUnitAnimationWithBlendingUnk reads 100.0 raw and normalised there, with no diff entry. Its callee InitBlendFilterArraysUnk reads 97.9 (95.0 normalised), as on main, with the same two lines as there: its loop ends just past the table's end (0x00D814DA and 0x00D814DC), which reccmp names after the DLL symbol that lies there on our side. The other 48 TextureRenderCore functions and ApplyBlending read exactly what they read before. Against main's own build (made at f489b72, 2,439 functions) the only other changes are main's since then (One more UI::MenuModals function at 100%: ProgressBarBox #467's MenuModalRenderFunction_ProgressBarBox and its constructor, UI: the single-player game launch at 100% #466's LaunchSinglePlayerGameUnk), and every other function reads exactly what it read before (unrounded 99.5790 raw and 99.5529 normalised, 2,326 functions at 100.0 raw; main 0e4a83b alone: 99.5788 and 99.5528 over 2,441, 2,325 at 100.0). The function-visibility.txt entry is used only by the wrapper-unit build (OPENSHC_FUNCTION_VISIBILITY_UNITS, off by default), so this /GL build does not exercise it.
  • Checks: our local style checker gives 0 ERROR and 48 CHECK (below); our local fixer finds nothing to change, and clang-format 23.1.3 changes nothing.
  • Not run in game.

What the function does

It draws one frame of a unit animation, blended. The TGX picture's pixels are byte indices into the sprite's player colour palette. The unit's owner 1 and 4 are swapped first, and colorPaletteOverride can replace the palette. Each looked-up colour is blended over the destination through two rows of DAT_BlendFilterArrays: row blendStrengthUnk weights the destination and row 32 - blendStrengthUnk the drawn colour. A strength of 32 or more draws nothing. The four drawing loops (RGB555 and RGB565, zoomed out and not) are the original's handwritten assembly, as inline asm. The zoomed-out ones draw every second row and skip the odd columns, from xPosition & 1 on.

Behaviour against the draft

The rest does what the draft does.

What got it to 100%

Each of these was measured in a narrow build against the alternative it names:

  • The zoomed-out path in an else block, with the column offset declared inside it. This is what moved the stack slots. The original's order is the line width at -4, the start height at -8, the palette at -12, the column offset at -16, then the two filter rows and the column parity. With the column offset declared at the top, it took -4 and moved each of the other three down one slot. The compiler's slot order follows each local's reference weight (main's tools/compiler-internals/slot_probe.py reads the weights from the compiler). An __asm block adds to the weight of every local that any asm block in the function writes, as long as that local is declared in a block that contains the asm block, wherever in that block the declaration stands. The column offset is written only in the zoomed-out blocks, but declared at the top of the function it counted at all four blocks, so it weighed 10, against 7, 7 and 6 for the other three. In the else block it weighs 6 and comes after the palette, as in the original. Declared at the top in this code, it reads 95.8; Reimplement UI::Rendering TextureRenderCore's renderUnitAnimationWithBlendingUnk from gynt's asm draft (draft, below 100%) #363's file with only this change reads 96.0. Declaring it lower down at function level changes nothing (the probe reads 10 either way); only a block scope does.
  • /GL with InitBlendFilterArraysUnk (on main's /GL list already, with entries for renderGmWithPreparedAlphaMask and renderInterfaceOrBuildingOccupationArea in function-visibility.txt). With the callee's body visible, the compiler keeps height in edi across the call as the original does (0x00451E3E to 0x00451EEB), and keeps the two surface arms' stores apart (0x00451EBD and 0x00451ED9). Without the /GL line this code reads 96.1 (71.2 normalised): height is loaded again behind the call, and blendStrengthUnk is in edi instead of ebx.
  • The order of the stores. In the map arm the heights come after the two line steps. With them first, it reads 99.9 (the start height's store moves up, 0x00451EC3). In the zoomed-out path the column parity comes first. After the heights it reads 98.8: the start height's store moves up, the parity's and and its store move down, and three registers swap roles (0x00452340 to 0x0045236C).

Changes against #363's code (same code)

  • The parameters take the header's names (xPosition, yPosition, imageAddress), and isZoom2 is tested by truthiness, as everywhere on main.
  • The token tests in the asm use main's TT_* constants (TgxToken.hpp), as main's renderGmWithPreparedAlphaMask does. The colour masks are written as C hex and the shift counts in decimal.
  • The labels say what they do (skip_token_555, draw_row_zoomed_565, ...) instead of a5skip, b6row.
  • Names without the leading underscore. lineByteWidth is now lineBytes, as in renderGmWithPreparedAlphaMask. The palette and the two filter rows are declared where they are first assigned, and the filter rows are written as array indexing (DAT_BlendFilterArrays::instance[blendStrengthUnk][0]) instead of byte offsets.
  • Each __asm block sits between // clang-format off and // clang-format on, as in main's other TextureRenderCore asm files.
  • No return before the else. The draft's 15-line NOTE comment is cut to one line; what it said is in this text.

Why the loops are inline assembly

Every token read starts with mov ecx, 0 before a byte load into cl, and no compiler materialises zero that way. The blocks compare with cmp reg, 0 instead of test, load the same token byte into two registers, and use mul for the row offset. Each block's start (the x test, the clipping against the start height) has the same tells (cmp eax, 0), so it stays in the block. The surface choice, the clamp of the height and the filter rows are C++.

Checker CHECKs (not changed)

  • 38 style-lint findings (36 of rule 18, 2 of rule 11): the hex masks in the asm. These are the RGB555 and RGB565 channel masks (0xffe0, 0xfc1f, 0x3ff, 0x83ff, 0xf81f, 0x7ff), and 0xfffffffe, which rounds xPosition down to an even column on the zoomed-out surface. Main's renderGmWithPreparedAlphaMask, renderInterfaceOrBuildingOccupationArea and ApplyBlending write these masks the same way (Reimplement 2 UI::Rendering TextureRenderCore asm functions from gynt's drafts (100%) #389 went in with 50 such findings). In decimal the bit layout would be lost.
  • 4 x SG 24 (declared without a value): lineBytes, heightStart and heightEnd are assigned in both paths and in both surface arms and read after them, so they are declared before the if; the asm reads the first two from their stack slots. columnOffset is written only by the asm.
  • 4 x inline asm block: see above.
  • cmake/function-visibility.txt: the entry for this function's callee under /GL, between renderGmWithPreparedAlphaMask and transformTilesetToRGB565 by address.
  • 1 x the bot's summary line.

Proposed, not changed here

  • TextureRenderCore::colorPaletteOverride is a dword in main's header. This function reads it as a palette (short*), so the code casts it. Proposed: short*. Not changed: on main only the constructor writes it (0), and its other writers in the exe aren't reimplemented yet.
  • blendStrengthUnk is the blend strength from 0 to 31. From the surface choice on, the function reuses it as the line step the asm adds per row, as the original does (0x00451EBD). Proposed name: blendStrength.
  • 4056: as in UI: TextureRenderCore's moveOverlappingMenuPartsToMapSurface at 100% #476's text, VC_MAP_SURFACE_WIDTH = 4056 would name the map surface's width here and on the 35 lines of main's .cpp files that write 4056.

Other PRs

🤖 Generated with Claude Code

TextureRenderCore::renderUnitAnimationWithBlendingUnk: blends a unit animation's TGX picture into the map or menu
surface through the player's colour palette and a pair of rows of the blend filter arrays, picked by the blend
strength. The four drawing loops (RGB555 and RGB565, zoomed out and not) are the original's handwritten assembly,
as inline asm. From gynt's asm draft: his 91.5% 3c9bcdb ("Four asm loops recovered, stack slot allocation
differs") on sourcehold's reimpl/TextureRenderCore, through our draft in sourcehold#363 (92.5%, 29ca840), which fixed his
two zoomed-path blocks drawing through the picture width as the surface pointer. His newest version, 3d17527
(2026-11-02, with d90dce4's style pass on top) on his fork's reimpl/UI-MenuItems-development, is the
decompiler's C form (6.0%, no asm); not used.

What the match needed:
- The zoomed path in an else block, with the column offset declared in it. The stack slots of the asm-referenced
  locals follow their reference weights, and every asm block adds to the weight of each local that any asm block
  writes, if that local is declared in a block that contains it (wherever in that block). At the top the column
  offset weighs more than the line width, the start height and the palette and takes their slots; in the zoomed
  block it falls behind them, as in the original (at the top: reccmp 95.8).
- /GL with InitBlendFilterArraysUnk (100% on main): with its body visible the compiler keeps height in edi across
  the call, as the original does, and keeps the two surface arms' stores apart.
- The map arm's stores with the heights last (with them first: 99.9), and the zoomed path's column parity first
  (after the heights: 98.8).

Also: the header's parameter names, isZoom2 tested by truthiness, the TGX token constants and C hex in the asm,
labels by what they do, no leading underscores, the filter rows as array indexing, declarations at their first
assignment where both paths don't share them. Same code.

Measured in a narrow build of this file with InitBlendFilterArraysUnk on main 0e4a83b, both resolver sets on:
100.0% raw and normalised. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

38 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
11 No magic addresses 2
18 Use the name, not the number 36
Where

Rule 11 — No magic addresses

  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:470 — literal address 0xFFFFFFFE
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:705 — literal address 0xFFFFFFFE

Rule 18 — Use the name, not the number

  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:172 — hex constant 0xFFE0; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:178 — hex constant 0xFC1F; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:184 — hex constant 0x3FF; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:194 — hex constant 0xFFE0; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:200 — hex constant 0xFC1F; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:206 — hex constant 0x83FF; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:228 — hex constant 0xFFE0; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:234 — hex constant 0xFC1F; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:240 — hex constant 0x3FF; use a name, or decimal
  • src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp:249 — hex constant 0xFFE0; use a name, or decimal
  • ... and 26 more

@gynt
gynt added this pull request to the merge queue Nov 24, 2026
Merged via the queue into sourcehold:main with commit 1143eb6 Nov 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants